package com.tjian.fafa.user.base.chatServices;

import android.util.SparseArray;
import android.util.SparseIntArray;

import com.tjian.fafa.user.base.bean.ChatEntity;
import com.tjian.fafa.user.base.bean.UserInfo;
import com.tjian.util.TypeUtil;

import java.util.ArrayList;
import java.util.List;

import static android.media.CamcorderProfile.get;


public class ChatServiceData
{

    private static ChatServiceData chatServiceData;

    /* List for friend chatting */
    private List<Integer> mFriendIds;
    private SparseArray<ArrayList<ChatEntity>> chatEntity_arrayList_sparseArray;
    private SparseArray<ArrayList<Boolean>> mMapFriendsSelf;
    private SparseIntArray mMapFriendsUnread;

    public static ChatServiceData getInstance()
    {
        if (chatServiceData == null)
        {
            chatServiceData = new ChatServiceData();
        }
        return chatServiceData;
    }

    private ChatServiceData()
    {

        mFriendIds = new ArrayList<Integer>();
        chatEntity_arrayList_sparseArray = new SparseArray<ArrayList<ChatEntity>>();
        mMapFriendsSelf = new SparseArray<ArrayList<Boolean>>();
        mMapFriendsUnread = new SparseIntArray();
    }

    public void newUser(UserInfo userInfo)
    {
        int id = userInfo.getId();

        mFriendIds.add(id);
        chatEntity_arrayList_sparseArray.put(id, new ArrayList<ChatEntity>());
        mMapFriendsSelf.put(id, new ArrayList<Boolean>());
        mMapFriendsUnread.put(id, 0);

    }

    public void offLineUser(UserInfo userInfo)
    {
        int id = userInfo.getId();

        for (int i = 0; i < mFriendIds.size(); i++)
        {
            if (mFriendIds.get(i).intValue() == id)
            {
                mFriendIds.remove(i);
                chatEntity_arrayList_sparseArray.remove(i);
                mMapFriendsSelf.remove(i);
                mMapFriendsUnread.removeAt(i);

                break;
            }
        }
    }

    public int getIdForPos(int pos)
    {
        int id = Integer.valueOf(mFriendIds.get(pos));
        return id;
    }

    public List<ChatEntity> getCurMsg(int type, int id)
    {
        if (type == 2)
        {
            ArrayList<ChatEntity> chatEntityArrayList = chatEntity_arrayList_sparseArray.get(id);
            //System.out.println("==========,_20161012_111032,\n" + TypeUtil.typeToString("chatEntityArrayList4", chatEntityArrayList));
            //System.out.println("==========,_20161012_111032,\n" + TypeUtil.typeToString("123", 123));
            return chatEntityArrayList;
        }
        else
        {
            return null;
        }
    }

    public List<Boolean> getCurIsSelf(int type, int id)
    {
        if (type == 2)
        {
            return mMapFriendsSelf.get(id);
        }
        else
        {
            return null;
        }
    }

    public void increUnreadMsgs(int id)
    {
        mMapFriendsUnread.put(id, mMapFriendsUnread.get(id) + 1);
    }

    public void clearUnreadMsgs(int id)
    {
        mMapFriendsUnread.put(id, 0);
    }

    public void setUnreadMsgs(int id, int am)
    {
        mMapFriendsUnread.put(id, am);
    }

    public int getUnreadMsgs(int id)
    {
        return mMapFriendsUnread.get(id);
    }

    public static void closeChatServiceData()
    {
        chatServiceData = null;
    }
}